﻿using System;
using System.Threading;
using System.Windows.Forms;

namespace NFC_Tag_Management.nfc.helper
{
    class AsyncDemo
    {
      string LongRunningMethod (int iCallTime, out int iExecThread)
	{
		Thread.Sleep (iCallTime) ;
		iExecThread = AppDomain.GetCurrentThreadId ();
		return "MyCallTime was " + iCallTime.ToString() ;
	}

	delegate string MethodDelegate(int iCallTime, out int iExecThread)  ;

	public void DemoSyncCall()
	{
		string s ;
		int iExecThread;

		// Create an instance of a delegate that wraps LongRunningMethod.
		MethodDelegate dlgt = new MethodDelegate (this.LongRunningMethod) ;  
			
		// Call LongRunningMethod using the delegate.
		s = dlgt(3000, out iExecThread);  
			
		MessageBox.Show (string.Format ("The delegate call returned the string:   \"{0}\", and the thread ID {1}", s, iExecThread.ToString() ) );
	}




        public void DemoWaitHandle ()
        {
           
	        string s ;
	        int iExecThread;

	        MethodDelegate dlgt = new MethodDelegate (this.LongRunningMethod) ;

	        // Initiate the asynchronous call.
	        IAsyncResult ar = dlgt.BeginInvoke(3000, out iExecThread, null, null); 

	        // Do some useful work here. This would be work you want to have
	        // run at the same time as the asynchronous call.

	        // Wait for the WaitHandle to become signaled.
	        ar.AsyncWaitHandle.WaitOne() ;

	        // Get the results of the asynchronous call.
	        s = dlgt.EndInvoke (out iExecThread, ar) ;
        			
	        MessageBox.Show (string.Format ("The delegate call returned the string:   \"{0}\", and the number {1}", s, iExecThread.ToString() ) );
        }

    }
   
}
